package cn.bincker.mybatis.encrypt.reflection.factory.interceptor;

import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bind.annotation.*;
import org.apache.ibatis.reflection.property.PropertyNamer;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

@Slf4j
public class SetterInterceptor {
    @SuppressWarnings("unused")
    @RuntimeType
    public static Object intercept(
            @This Object that,
            @Origin Method method,
            @SuperCall Callable<?> superCall,
            @FieldValue(SetEncryptFutureInterceptor.encryptFutureFieldName)Map<String, Future<?>> futureMap
    ) {
        try {
            if (futureMap != null) {
                futureMap.remove(PropertyNamer.methodToProperty(method.getName()));
            }
            return superCall.call();
        }catch (Throwable e){
            throw new RuntimeException(e);
        }
    }
}
